<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<style>
    * {
        margin: 0;
        padding: 0;
    }
    
    ul li {
        list-style: none;
    }
    
    main {
        width: 960px;
        height: 500px;
        border-radius: 10px;
        margin: 50px auto;
        border: 2px solid black;
    }
    
    main h4 {
        height: 100px;
        line-height: 100px;
        text-align: center;
    }
    
    .tabsbox {
        width: 900px;
        margin: 0 auto;
        height: 400px;
        border: 1px solid lightsalmon;
        position: relative;
    }
    
    nav ul {
        overflow: hidden;
    }
    
    nav ul li {
        float: left;
        width: 100px;
        height: 50px;
        line-height: 50px;
        text-align: center;
        border-right: 1px solid #ccc;
        position: relative;
    }
    
    nav ul li.liactive {
        border-bottom: 2px solid #fff;
        z-index: 9;
    }
    
    #tab input {
        width: 80%;
        height: 60%;
    }
    
    nav ul li span:last-child {
        position: absolute;
        user-select: none;
        font-size: 12px;
        top: -18px;
        right: 0;
        display: inline-block;
        height: 20px;
    }
    
    .tabadd {
        position: absolute;
        /* width: 100px; */
        top: 0;
        right: 0;
    }
    
    .tabadd span {
        display: block;
        width: 20px;
        height: 20px;
        line-height: 20px;
        text-align: center;
        border: 1px solid #ccc;
        float: right;
        margin: 10px;
        user-select: none;
    }
    
    .tabscon {
        width: 100%;
        height: 300px;
        position: absolute;
        padding: 30px;
        top: 50px;
        left: 0px;
        box-sizing: border-box;
        border-top: 1px solid #ccc;
    }
    
    .tabscon section,
    .tabscon section.conactive {
        display: none;
        width: 100%;
        height: 100%;
    }
    
    .tabscon section.conactive {
        display: block;
    }
    
    @font-face {
        font-family: "iconfont";
        src: url('./iconfont/iconfont.eot?t=1553960438096');
        /* IE9 */
        src: url('./iconfont/iconfont.eot?t=1553960438096#iefix') format('embedded-opentype'), /* IE6-IE8 */
        url('data:application/x-font-woff2;charset=utf-8;base64,d09GMgABAAAAAAK4AAsAAAAABmwAAAJrAAEAAAAAAAAAAAAAAAAAAAAAAAAAAAAAHEIGVgCCcAp4fwE2AiQDCAsGAAQgBYRtBzAbpQXIrrApw71oi3CCOyzEy8RvE4yIN8TD036/zp03qCYRjaJZNBFFS/gREoRGipQKofjuNrb+9XbTqrmXcqWzfTRDqFqWkhAJzYToaE6LQ7Q30CirRqSKMnj58DdIdrNAdhoTQJa5VGfLrtiAy+lPoAcZdUC57UljTR4TMAo4oL0xiqwYG8YueIHPCdTqYajty/t+bUpmrwvEnUK42lQhLMssVy1UNhzN4kmF6vSQVvMY/T5+HEU1SUXBbti7uBBrx++cgqJULp0GhAgBna5AgSkgE0eN6R1NwTitNt0yAI5VG7wr/8AljmoX7K+zq+tBF1Q8k9JTPWp1AjnJDgCzmM3bU0V31dsvV3M2eC6fHjaGfX/qS7U5Gr58vj6uD0bgxudyrV/OtHHyP+NZnpO1txbktjdY+3FB61+7nxeOzq8niGYnRwT3v3aZxeXf6rrNxl5//49WlEtZUUL1Pj3Bv1EO7MuG2namrCkbvcnApLUJtWpRhv2tzlRLx43kQ7WO2/FW6c5QqDZEZnYKFeosoVK1NdSa5E/XaVM1Ra7BhAEQmk0kjV5QaLbIzG5U6HRRqTkK1DqJtivrjMT1zJaNnIsihAiyQE3JdbszcW0Xiadzdl4d8UO0HSUGNDNXzl2hifYSO5pPjrorgdjUAAavoa5TKDZVUXD3kuuOOzh70fShvUiN2owtNsRxIREIIiATUCYpGO2aqXy/CxEeHcfuaKrLDiGbQ5kcEMsNIK8M5qCmR3mn8RFHOpcECBtlAAwWIZ2OAqV5kQoJXHvShORYBzrDZKhhb3uT8QPlrA3bmsKZV6i89DiTV2o1AAAA') format('woff2'), url('./iconfont/iconfont.woff?t=1553960438096') format('woff'), url('./iconfont/iconfont.ttf?t=1553960438096') format('truetype'), /* chrome, firefox, opera, Safari, Android, iOS 4.2+ */
        url('./iconfont/iconfont.svg?t=1553960438096#iconfont') format('svg');
        /* iOS 4.1- */
    }
    
    .iconfont {
        font-family: "iconfont" !important;
        font-size: 16px;
        font-style: normal;
        -webkit-font-smoothing: antialiased;
        -moz-osx-font-smoothing: grayscale;
    }
    
    .icon-guanbi:before {
        content: "\e676";
    }
</style>

<body>
    <main>
        <h4>
            Js 面向对象 动态添加标签页
        </h4>
        <div class="tabsbox" id="tab">
            <!-- tab 标签 -->
            <nav class="firstnav">
                <ul>
                    <li class="liactive"><span>测试1</span><span class="iconfont icon-guanbi"></span></li>
                    <li><span>测试2</span><span class="iconfont icon-guanbi"></span></li>
                    <li><span>测试3</span><span class="iconfont icon-guanbi"></span></li>
                </ul>
                <div class="tabadd">
                    <span>+</span>
                </div>
            </nav>

            <!-- tab 内容 -->
            <div class="tabscon">
                <section class="conactive">测试1</section>
                <section>测试2</section>
                <section>测试3</section>
            </div>
        </div>
    </main>
    <script>
        var that;
        class Tab {
            constructor(id) {
                    //获取元素
                    that = this;
                    this.main = document.querySelector(id);

                    this.add = this.main.querySelector('.tabadd');
                    //li的父元素
                    this.ul = this.main.querySelector('.firstnav ul:first-child');
                    //section的父元素
                    this.fsection = this.main.querySelector('.tabscon');
                    this.init();
                }
                //初始化 绑定事件
            init() {
                    that.updateNode();
                    this.add.onclick = this.addTab;
                    for (var i = 0; i < this.lis.length; i++) {
                        this.lis[i].index = i;
                        this.lis[i].onclick = this.toggleTab;
                        this.remove[i].onclick = this.removeTab;
                        this.spans[i].ondblclick = this.editTab;
                        this.sections[i].ondblclick = this.editTab;
                    }
                }
                //获取所有的小li和section
            updateNode() {
                    this.lis = this.main.querySelectorAll('li');
                    this.sections = this.main.querySelectorAll('section');
                    this.remove = this.main.querySelectorAll('.icon-guanbi');
                    this.spans = this.main.querySelectorAll('.firstnav li span:first-child');
                }
                //1切换
            toggleTab() {
                that.clearClass();
                this.className = 'liactive';
                that.sections[this.index].className = 'conactive';
            }
            clearClass() {
                    for (let i = 0; i < this.lis.length; i++) {
                        this.lis[i].className = '';
                        this.sections[i].className = '';
                    }
                }
                //2添加
            addTab() {
                    that.clearClass();
                    //1创建li元素
                    var li = '<li class="liactive"><span>新选项卡</span><span class="iconfont icon-guanbi"></span></li>';
                    var section = '<section class="conactive">新内容</section>'
                        //2追加元素到父元素中
                    that.ul.insertAdjacentHTML('beforeend', li);
                    that.fsection.insertAdjacentHTML('beforeend', section);
                    that.init();
                }
                //3删除
            removeTab(e) {
                    e.stopPropagation();
                    var index = this.parentNode.index;
                    console.log(index);
                    //根据索引号删除对应的li和section
                    that.lis[index].remove();
                    that.sections[index].remove();
                    that.init();
                    //当我们删除的不是选中状态的li的时候 原来的选中状态保持不变
                    if (document.querySelector('.liactive')) return;
                    //当我们删除了选中状态的li的时候，让它的前一个li处于选中状态
                    index--;
                    that.lis[index] && that.lis[index].click();

                }
                //4修改
            editTab() {
                var str = this.innerHTML;
                //双击禁止选中文字
                window.getSelection ? window.getSelection().removeAllRanges() : document.selection.empty();
                this.innerHTML = '<input type="text" />';
                var input = this.children[0];
                input.value = str;
                input.select(); //文本框中的文字处于选中状态
                //当光标离开就把文本框的值给span
                input.onblur = function() {
                        this.parentNode.innerHTML = this.value;
                    }
                    //按下回车也可赋值
                input.onkeyup = function(e) {
                    if (e.keyCode === 13) {
                        this.blur();
                    }
                }
            }
        }
        new Tab('#tab');
    </script>
</body>

</html>